﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace cz.burger.windows.MDCCTaxi
{
    class WebParser
    {
        internal bool IsLoginForm(string content) 
        {
            if (!new Regex(@"<input\s+name=""username""\s").IsMatch(content)) return false;
            if (!new Regex(@"<input\s+name=""password""\s").IsMatch(content)) return false;

            var formTag = new Regex(@"<form.+?>").Match(content);
            if (formTag.Captures.Count != 1) return false;

            var formAction = new Regex(@"action=""(.+?)""").Match(formTag.Captures[0].Value);
            if (formAction.Captures.Count != 1) return false;

            return true;
        }

        internal bool GetLoginAction(string content, out string formAction)
        {
            formAction = String.Empty;

            var formTag = new Regex(@"<form.+?>").Match(content);
            if (formTag.Captures.Count != 1) return false;

            var formActionMatch = new Regex(@"action=""(.+?)""").Match(formTag.Captures[0].Value);
            if (formActionMatch.Captures.Count != 1) return false;

            formAction = formActionMatch.Groups[1].Value;
            return true;
        }

        internal bool IsOrderPage(string content) 
        {
            return new Regex(@"fill out the form").IsMatch(content);
        }

        internal bool GetMenuLinks(string content, out string orderLink, out string statusLink) 
        {
            // menu = 4 subsequent anchors with images inside -> new order / status / password / logout
            var menu = new Regex(@"<a.+?><img.+?></a>").Matches(content);
            if (menu.Count < 2)
            {
                orderLink = String.Empty;
                statusLink = String.Empty;
                return false;
            }

            var hrefRegex = new Regex(@"href=""(.+?)""");
            orderLink = hrefRegex.Match(menu[0].Captures[0].Value).Groups[1].Value;
            statusLink = hrefRegex.Match(menu[1].Captures[0].Value).Groups[1].Value;

            return true;

        }

        internal bool GetRideStrings(string content, out List<string> rideStrings)
        {
            rideStrings = new List<string>();

            var table = new Regex(@"<table\s+id=""fares"".+?\/table>", RegexOptions.Singleline).Match(content);
            if (table.Captures.Count != 1) return false;

            var rides = new Regex(@"<tr.+?\/tr>", RegexOptions.Singleline).Matches(table.Captures[0].Value);

            foreach (Match ride in rides)
            {
                rideStrings.Add(ride.Captures[0].Value);
            }
            return true;
        }

        internal bool GetRide(string row, out RideData ride)
        {
            ride = null;
            var imgRegex = new Regex(@"<img.+?src=""inc/gfx/car_(\d\d?).gif"".+?title=""(.+?)"".+?<\/td>.*?<td><span.+?>(.+?)<\/span>", RegexOptions.Singleline);
            var match = imgRegex.Match(row);
            if (match == null || match.Groups.Count < 4) return false;

            ride = new RideData() { 
                Code = Convert.ToInt32(match.Groups[1].Value), 
                Title = match.Groups[2].Value,
                Time = match.Groups[3].Value
            };
            return true;
        }

    }
}
